<?php
namespace app\controller;

use app\BaseController;
use think\facade\Request;
use think\facade\Db;
use think\Exception;

class GoodsInfo extends BaseController
{

    //获取商品信息大类名称
    public function get_GoodsInfo()
    {
        $data=[
            [
                'name'=>'商品种类',
                'table'=>'SET_GoodsCategory'
            ],
            [
                'name'=>'商品名称',
                'table'=>'SET_GoodsName'
            ],
            [
                'name'=>'品牌',
                'table'=>'SET_GoodsBrand'
            ],
            [
                'name'=>'成色',
                'table'=>'SET_GoodsGrade'
            ],
            [
                'name'=>'系列',
                'table'=>'SET_GoodsSeries'
            ],


        ];

        return json([
            'code'=>200,
            'msg'=>'获取成功',
            'data'=>$data
        ]);

    }

    //获取商品信息明细表格
    public function get_GoodsInfoTable()
    {
        $table=Request::param('table');
        $current_page = Request::param('current_page');
        $page_size = Request::param('page_size');

        $userId = $this->request->auth['data']->user->user_id;

        // 根据用户ID获取公司ID
        $companyId = Db::name('user')->where('user_id', $userId)->value('user_company_id');

        $header=[];

        switch ($table) {

            case 'SET_GoodsCategory':
                $header=[
                    [
                        'prop'=>'GoodsCategory_name',
                        'lable'=>'种类名称',

                    ],
                    [
                        'prop'=>'GoodsCategory_weightunit',
                        'lable'=>'重量单位',

                    ],

                    [
                        'switch'=>true,
                        'prop'=>'GoodsCategory_switch',
                        'lable'=>'开关',

                    ],

                ];
                break;
            case 'SET_GoodsName':
                $header=[
                    [
                        'prop'=>'GoodsName_name',
                        'lable'=>'商品名称',

                    ],

                    [
                        'switch'=>true,
                        'prop'=>'GoodsName_switch',
                        'lable'=>'开关',

                    ],

                ];
                break;
            case 'SET_GoodsBrand':
                $header=[
                    [
                        'prop'=>'GoodsBrand_name',
                        'lable'=>'品牌名称',

                    ],
                    [
                        'prop'=>'GoodsBrand_prefix',
                        'lable'=>'前缀',

                    ],
                    [
                        'prop'=>'GoodsBrand_remark',
                        'lable'=>'备注',

                    ],

                    [
                        'switch'=>true,
                        'prop'=>'GoodsBrand_switch',
                        'lable'=>'开关',

                    ],

                ];
                break;
            case 'SET_GoodsGrade':
                $header=[
                    [
                        'prop'=>'GoodsGrade_name',
                        'lable'=>'成色名称',

                    ],
                    [
                        'prop'=>'GoodsGrade_remark',
                        'lable'=>'备注',

                    ],

                    [
                        'switch'=>true,
                        'prop'=>'GoodsGrade_switch',
                        'lable'=>'开关',

                    ],

                ];
                break;
            case 'SET_GoodsSeries':
                $header=[
                    [
                        'prop'=>'GoodsSeries_name',
                        'lable'=>'系列名称',

                    ],
                    [
                        'prop'=>'GoodsSeries_remark',
                        'lable'=>'备注',

                    ],

                    [
                        'switch'=>true,
                        'prop'=>'GoodsSeries_switch',
                        'lable'=>'开关',

                    ],

                ];
                break;


        }

        $data = Db::table($table)->where(substr($table, strlen('SET_')).'_company_id',$companyId)->page($current_page,$page_size)->select();
        $count = Db::table($table)->where(substr($table, strlen('SET_')).'_company_id',$companyId)->count();

        return json([
            'code'=>200,
            'msg'=>'获取成功',
            'header'=>$header,
            'data'=>[
                'data'=>$data,
                'total'=>$count,
            ]
        ]);


    }

    //新增商品信息
    public function insert_GoodsInfoTable()
    {
        $table = $this->request->param('table');
        $data = $this->request->param('data');
        $userId = $this->request->auth['data']->user->user_id;

        // 根据用户ID获取公司ID
        $companyId = Db::name('user')->where('user_id', $userId)->value('user_company_id');

        $data[substr($table, strlen('SET_')).'_company_id']=$companyId;
        $data[substr($table, strlen('SET_')).'_switch']=1;

        //判断商品名称是否重复
        if(Db::table($table)->where(substr($table, strlen('SET_')).'_name',$data[substr($table, strlen('SET_')).'_name'])->where(substr($table, strlen('SET_')).'_company_id',$companyId)->find())
        {

                return json([
                    'code'=>501,
                    'msg'=>'名称重复'
                ]);

        }

        try{
            Db::startTrans();
            $res = Db::table($table)->insert($data);
            Db::commit();
            if($res){
                return json([
                    'code'=>200,
                    'msg'=>'新增成功',
                    'data'=>$res
                ]);
            }else{
                return json([
                    'code'=>500,
                    'msg'=>'新增失败',
                    'data'=>$res
                ]);
            }
        }catch (Exception $e){
            // 回滚事务
            Db::rollback();
            return json([
                'code'=>500,
                'msg'=>'新增失败'.$e->getMessage(),
                'data'=>$e->getMessage()
            ]);
        }

    }

    //修改商品信息
    public function update_GoodsInfoTable()
    {
        $id = $this->request->param('id');
        $data = $this->request->param('data');
        $table = $this->request->param('table');
        $userId = $this->request->auth['data']->user->user_id;

        // 根据用户ID获取公司ID
        $companyId = Db::name('user')->where('user_id', $userId)->value('user_company_id');

        try {
            Db::startTrans();

            //检查$data是否包含switch属性,如果包含则跳过代码块
            if (isset($data['switch']))
            {

            $info = Db::table($table)
                ->where(substr($table, strlen('SET_')).'_id', '<>', $id['value'])
                ->where(substr($table, strlen('SET_')).'_name', $data[substr($table, strlen('SET_')).'_name'])
                ->where(substr($table, strlen('SET_')).'_company_id', $companyId)
                ->find();

            if ($info) {
                return json([
                    'code' => 400,
                    'msg' => '商品名称已存在',
                ]);
            }

            }


            $res = Db::table($table)->where(substr($table, strlen('SET_')).'_id',$id['value'])->update($data);

            if($res){
                Db::commit();
                return json([
                    'code'=>200,
                    'msg'=>'修改成功',
                    'data'=>$res
                ]);
            }else{
                Db::rollback();
                return json([
                    'code'=>500,
                    'msg'=>'修改失败',
                    'data'=>$res
                ]);

            }
        } catch (Exception $e){
            // 回滚事务
            Db::rollback();
            return json([
                'code'=>500,
                'msg'=>'修改失败'.$e->getMessage(),
                'data'=>$e->getMessage()
            ]);
        }
    }

    //删除商品信息
    public function delete_GoodsInfoTable(){
        $id = $this->request->param('id');
        $table = $this->request->param('table');

        try {
            Db::startTrans();
            $res = Db::table($table)->where(substr($table, strlen('SET_')).'_id',$id)->delete($id);
            if($res){
                Db::commit();
                return json([
                    'code'=>200,
                    'msg'=>"删除成功",
                    'data'=>$res
                ]);
            }else{
                Db::rollback();
                return json([
                    'code'=>500,
                    'msg'=>"删除失败",
                    'data'=>$res
                ]);

            }
        }
        catch (Exception $e){
            // 回滚事务
            Db::rollback();
            if($e->getCode()==10501)
            {
                return json(['code' => 500, 'msg' => '该商品信息下已存在单据，无法删除']);
            }
            return json([
                'code'=>500,
                'msg'=>'删除失败'.$e->getMessage(),
                'data'=>$e->getMessage()
            ]);
        }

    }
}